//
//  UIViewController+Dealloc.m
//  RuntimeDemo
//
//  Created by LeeJay on 16/8/8.
//  Copyright © 2016年 LeeJay. All rights reserved.
//  代码下载地址https://github.com/leejayID/Linkage
//
#import "UIViewController+Dealloc.h"
#import <objc/runtime.h>

#import "ShopCarController.h"
#import "GoodsSkuListModel.h"
@implementation UIViewController (Dealloc)

#pragma mark - swizzle
+ (void)load
{
    Method method1 = class_getInstanceMethod([self class], NSSelectorFromString(@"dealloc"));
    Method method2 = class_getInstanceMethod([self class], @selector(deallocSwizzle));
    method_exchangeImplementations(method1, method2);
}

- (void)deallocSwizzle
{
    
    
    NSLog(@"%@控制被销毁了", NSStringFromClass([self class]));
    
    [self deallocSwizzle];
}

static char MethodKey;
- (void)setMethod:(NSString *)method
{
    objc_setAssociatedObject(self, &MethodKey, method, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (NSString *)method
{
    return objc_getAssociatedObject(self, &MethodKey);
}


- (NSInteger)calculateTotalGoodsCounts{
    
    NSInteger counts = 0;
    for (int i=0; i < [UserViewModel shareUserViewModel].shopCar.shopCarList.count; i++) {
        GoodsSetionModel *setionModel = [[UserViewModel shareUserViewModel].shopCar.shopCarList objectAtIndex:i];
        
        for (int j = 0; j < setionModel.goods.count; j++) {
            GoodsSkuListModel *model = [setionModel.goods objectAtIndex:j];
            NSInteger count = [model.count integerValue];
            counts  = count + counts;
        }
    }
    
    if (counts > 0) {
        myViewModel.badgeValue = [NSString stringWithFormat:@"%ld",(long)counts];

    } else {
        myViewModel.badgeValue = nil;

    }
    
    
    return counts;
    
}



- (void)addGoodsWithModel:(GoodsSkuListModel *)model{
    
//    if ([model.item_info.sale_state isEqualToString:@"3"]) {
//
//        [CustomAlert showMessage:@"商品缺货,等待增加!"];
//
//
//        return;
//    }
    
    
    
    BOOL haveGood = NO;
    for (int i = 0; i < [UserViewModel shareUserViewModel].shopCar.shopCarList.count; i++) {
        if (haveGood == YES) break;
        GoodsSetionModel *temp = [UserViewModel shareUserViewModel].shopCar.shopCarList[i];
        for (int j = 0; j < temp.goods.count; j++) {
            
            GoodsSkuListModel *tempModel = temp.goods[j];
            
            if ([model.sku_id isEqualToString:tempModel.sku_id]) {
                
                NSInteger number =  tempModel.count.integerValue;
                
                number = number + 1;
                    
                    
                tempModel.count = [NSString stringWithFormat:@"%ld",(long)number];

                haveGood = YES;
                
                break;
            }
            
            
        }
        
        
    }
    
    
    
    
    if (haveGood == YES){
        
        [self updateShoppingCarView];
        
        return;
    }
    
    
    
    BOOL haveSection = NO;
    for (int i = 0; i < [UserViewModel shareUserViewModel].shopCar.shopCarList.count; i++) {
        if (haveSection == YES) break;
        GoodsSetionModel *temp = [UserViewModel shareUserViewModel].shopCar.shopCarList[i];

        
            if ([model.store_id isEqualToString:temp.store_id]) {
                
                GoodsSkuListModel *skuModel = [self creatOneListModel:model];
                
                [temp.goods insertObject:skuModel atIndex:0];
                
                haveSection = YES;
                
                break;
            }
            

        
        
    }
    
    
    
    
    
    if (haveSection == YES){
        
        [self updateShoppingCarView];
        
        return;
    }
    
    
    
    GoodsSetionModel *tempSetionModel = [[GoodsSetionModel alloc]init];
    
    tempSetionModel.goods = [NSMutableArray array];
    
    tempSetionModel.checked = @"1";
    
    GoodsSkuListModel *skuModel1 = [self creatOneListModel:model];
    
    [tempSetionModel.goods addObject:skuModel1];
    
    [[UserViewModel shareUserViewModel].shopCar.shopCarList addObject:tempSetionModel];


    [self updateShoppingCarView];
    
    
    
}

- (GoodsSkuListModel *)creatOneListModel:(GoodsSkuListModel *)model{
    
    GoodsSkuListModel *skuModel = [[GoodsSkuListModel alloc]init];
    
      skuModel.create_date = model.create_date;
      skuModel.update_date = model.update_date;
      skuModel.picture = model.picture;
      skuModel.cost_price = model.cost_price;
      skuModel.sku_id = model.sku_id;
      skuModel.stock = model.stock;
      skuModel.promote_price = model.promote_price;
      skuModel.market_price = model.market_price;
      skuModel.price = model.price;
      skuModel.attr_value_items_format = model.attr_value_items_format;
      skuModel.code = model.code;
      skuModel.QRcode = model.QRcode;
      skuModel.goods_id = model.goods_id;
      skuModel.member_price = model.member_price;
      skuModel.attr_value_items = model.attr_value_items;
      skuModel.sku_name = model.sku_name;
      skuModel.goods_name = model.goods_name;
      skuModel.goods_image_url = model.goods_image_url;
      skuModel.middelText = model.middelText;
      skuModel.isSelect = @"1";
      skuModel.count = @"1";
    
     return skuModel;


    
}




- (void)updateShoppingCarView{
    
    
    
//    ShopCarController *shopCarVC = (ShopCarController *)[UIApplication sharedApplication].keyWindow.rootViewController.childViewControllers[2].childViewControllers[0];
//
//
//    if ([shopCarVC isViewLoaded]) {
//
//        [shopCarVC calculateTotalPrice];
//
//    }
    
    
}



//消除警告
- (void)calculateTotalPrice{}
- (void)updateEndView{}


@end
